package com.shh.shojbackendjudgeservice.judge.strategy;

import com.shh.shojbackendmodel.model.codesandbox.JudgeInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * 封装选择判题策略
 * 由于选择判题策略需要很多if else判断，会增加代码冗余,编写一个独立的类来进行简化
 */
@Service
@Slf4j
public class JudgeManager {

    /**
     * 执行判题
     * @param judgeContext
     * @return
     */
    public JudgeInfo doJudge(JudgeContext judgeContext){
        //获取用户的语言
        String language = judgeContext.getQuestionSubmit().getLanguage();

        //默认为DefaultJudgeStrategy
        JudgeStrategy judgeStrategy = new DefaultJudgeStrategy();
        //根据语言选择不同的判题策略
        if("java".equals(language)){
            judgeStrategy = new JavaLanguageJudgeStrategy();
            log.info("开始执行java判题策略");
        }
        JudgeInfo judgeInfo = judgeStrategy.doJudge(judgeContext);
        log.info("判题完成 判题信息{} 判题时间{} 判题内存",judgeInfo.getMessage(),judgeInfo.getTime(),judgeInfo.getMemory());
        return judgeInfo;
    }
}
